<?php
namespace GRH\Model\Table;

use Zend\Db\Adapter\Adapter;
use Zend\Db\TableGateway\AbstractTableGateway;
use Zend\Db\ResultSet\ResultSet;

use Zend\Db\Sql\Sql;
use Zend\Db\Sql\Select;
use Zend\Db\Sql\Where;
use Zend\Db\Sql\Expression;

use GRH\Model\Employe;

class EmployeTable extends AbstractTableGateway
{
    protected $table = 'grh_employe';
    
    /**
     * Constructeur
     * 
     * @param \Zend\Db\Adapter\Adapter $adapter
     */
    public function __construct(Adapter $adapter)
    {
        $this->adapter = $adapter;
        $this->resultSetPrototype = new ResultSet();
        $this->resultSetPrototype->setArrayObjectPrototype(new Employe());
        $this->initialize();
    }
    
    /**
     * Tous les employés de tous les sites
     * 
     * @return type
     */
    public function getAll()
    {
        return $this->select();
    }
    
    /**
     * Tous les employés d'un site donné
     * 
     * @param type $site_id
     * @return type
     */
    public function getAllBySite($site_id = 1)
    {
        return $this->select(array('site_id' => $site_id));
    }
    
    public function get($id)
    {
                $id = (int)$id;
        $row = $this->table
                ->select(array('employe_id' => $id))
                ->current();
        if(!$row)
            throw new \Exception("Impossible de trouver '$id'");
        
        return $row;
    }
    
    /**
     * Enregistrer un employé
     * 
     * @param \GRH\Model\Employe $employe
     * @return type
     * @throws \Exception
     */
    public function save(Employe $employe)
    {
        $data = array(
            'nom' => $employe->nom,
            'prenom' => $employe->prenom,
            'login' => $employe->login,
            'mdp' => $employe->mdp,
            'adresse1' => $employe->adresse1,
            'adresse2' => $employe->adresse2,
            'date_naissance' => $employe->date_naissance,
            'lieu_naissance' => $employe->lieu_naissance,
            'nationalite' => $employe->nationalite,
            'titre_sejour' => $employe->titre_sejour,
            'num_titre_sejour' => $employe->num_titre_sejour,
            'num_cpam' => $employe->num_cpam,
            'num_telephone' => $employe->num_telephone,
            'num_mobile' => $employe->num_mobile,
            'email' => $employe->email,
            'situation_famille' => $employe->situation_famille,
            'mutuelle' => $employe->mutuelle,
            'num_pole_emploi' => $employe->num_pole_emploi,
            'contrat_id' => $employe->contrat_id,
            'transport_id' => $employe->transport_id,
            'type_employe_id' => $employe->type_employe_id,
            'fiche_medical' => $employe->fiche_medical,
        );
        $id = (int)$employe->employe_id;
        if($id == 0)
            $this->table->insert($data);
        else
            if($this->get($id))
                $this->table->update($data, array('employe_id' => $id));
            else
                throw new \Exception("Impossible de trouver '$id'");
            
        if($id == 0) return $this->lastInsertValue;
        else return $id;
    }
    
    /**
     * Effacer un employé
     * 
     * @param type $id
     */
    public function delete($id)
    {
        $this->table->delete(array('employe_id' => $id));
    }
}